package com.jy.adscm.operation.base.service.impl;

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.base.bean.TaskManagerAttchment;
import com.jy.adscm.operation.base.mapper.mysql.TaskManagerAttchmentMapper;
import com.jy.adscm.operation.base.service.TaskManagerAttchmentService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yyy
 * @since 2019-01-21
 */
@Service
public class TaskManagerAttchmentServiceImpl extends ServiceImpl<TaskManagerAttchmentMapper, TaskManagerAttchment> implements TaskManagerAttchmentService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Override
    public List<TaskManagerAttchment> selectByFileId(Integer fileId) {
        Wrapper<TaskManagerAttchment> wrapper =new EntityWrapper<>();
        wrapper.eq("tid",fileId);
        List<TaskManagerAttchment> taskManagerAttchments = super.selectList(wrapper);
        for (TaskManagerAttchment taskManagerAttchment : taskManagerAttchments) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(taskManagerAttchment.getUploadPerson(),null)){
                JyUser jyUser = jyUserMapper.selectById(taskManagerAttchment.getUploadPerson());
                taskManagerAttchment.setUploadPersonName(jyUser.getName());
            }
            
        }

        return taskManagerAttchments;
    }

    @Override
    public Boolean deleteTaskManagerAttchments(Integer id, Token token)throws AdscmSystemException {
        boolean delete= false;
        TaskManagerAttchment taskManagerAttchment = super.selectById(id);
        if (AdscmBaseUtils.getInstance().isNotEmpty(taskManagerAttchment.getUploadPerson(),null)){
            if (taskManagerAttchment.getUploadPerson().equals(token.getUser().getId().toString())){
                File  file=new File(taskManagerAttchment.getFilePath());
                if (file.exists()){
                    file.delete();
                }
                delete  = super.deleteById(id);
            }else {
                throw  new  AdscmSystemException("您不能删除！");
            }
        }
        return delete;
    }
}
